import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
import 'package:get/get.dart';
import 'package:mziot/app/config/design.dart';
import 'package:mziot/app/tools/tools.dart';
import 'package:mziot/generated/assets.dart';
import 'package:mziot/generated/locales.g.dart';
import 'package:r_upgrade/r_upgrade.dart';

class UpdateAppView extends StatelessWidget {
  final Map<String, dynamic> info;
  UpdateAppView(this.info, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool isForce = info['isMustUpdate'] ?? false;
    return WillPopScope(
      onWillPop: () {
        return Future.value(false);
      },
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          color: Colors.transparent,
        ),
        padding: EdgeInsets.only(left: 37, right: 37),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                Assets.imagesUpdatePop,
                width: Get.width - 74,
                fit: BoxFit.fill,
              ),
              Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(8),
                    bottomRight: Radius.circular(8),
                  ),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(height: 9),
                    Center(
                      child: Text(
                        '${info['newestVersion']}',
                        style: MFont.semi_Bold17.apply(color: MColor.xFF333333),
                      ),
                    ),
                    SizedBox(height: 12),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      child: Text(
                        info['updateContent'],
                        style: MFont.regular13.apply(color: MColor.xFF666666),
                      ),
                    ),
                    if (!isForce) ...{
                      SizedBox(height: 16),
                      Divider(height: 1, color: MColor.xFFCCCCCC),
                      Row(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Expanded(
                            child: GestureDetector(
                              behavior: HitTestBehavior.translucent,
                              child: Container(
                                margin: EdgeInsets.symmetric(vertical: 14),
                                child: Center(
                                  child: Text(
                                    LocaleKeys.update_cancel.tr,
                                    style: MFont.semi_Bold15.apply(color: MColor.xFF333333),
                                  ),
                                ),
                              ),
                              onTap: () {
                                Get.back();
                              },
                            ),
                          ),
                          Container(
                            color: MColor.xFFCCCCCC,
                            width: 0.5,
                            height: 50,
                          ),
                          Expanded(
                            child: GestureDetector(
                              behavior: HitTestBehavior.translucent,
                              child: Container(
                                margin: EdgeInsets.symmetric(vertical: 14),
                                child: Center(
                                  child: Text(
                                    LocaleKeys.update_go.tr,
                                    style: MFont.semi_Bold15.apply(color: MColor.xFF25C56F),
                                  ),
                                ),
                              ),
                              onTap: () {
                                final url = info['downLoadUrl'];
                                toUpdate(url);
                              },
                            ),
                          ),
                        ],
                      ),
                    } else ...{
                      SizedBox(height: 24),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 16),
                        child: GestureDetector(
                          behavior: HitTestBehavior.translucent,
                          child: Container(
                            height: 44,
                            width: double.infinity,
                            decoration: BoxDecoration(
                              color: MColor.xFF25C56F,
                              borderRadius: BorderRadius.circular(22),
                            ),
                            child: Center(
                              child: Text(
                                LocaleKeys.update_go.tr,
                                style: MFont.semi_Bold15.apply(color: Colors.white),
                              ),
                            ),
                          ),
                          onTap: () {
                            final url = info['downLoadUrl'];
                            toUpdate(url);
                          },
                        ),
                      ),
                      SizedBox(height: 16),
                    },
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void toUpdate(String url) {
    if (Platform.isAndroid) {
      RUpgrade.upgradeFromAndroidStore(AndroidStore.GOOGLE_PLAY).then((value) {
        if (!(value ?? false)) {
          showToast(LocaleKeys.update_failed.tr);
        }
      });
    } else {
      RUpgrade.upgradeFromAppStore('1612237275');
    }
  }
}
